package top.itmeans.yes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import top.itmeans.yes.entity.HomeRecommendSubject;
import top.itmeans.yes.mapper.HomeRecommendSubjectMapper;
import top.itmeans.yes.service.HomeRecommendSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页推荐专题表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class HomeRecommendSubjectServiceImpl extends ServiceImpl<HomeRecommendSubjectMapper, HomeRecommendSubject> implements HomeRecommendSubjectService {
    @Resource
    private HomeRecommendSubjectMapper recommendProductMapper;
    @Override
    public int create(List<HomeRecommendSubject> recommendSubjectList) {
        for (HomeRecommendSubject recommendProduct : recommendSubjectList) {
            recommendProduct.setRecommendStatus(1);
            recommendProduct.setSort(0);
            recommendProductMapper.insert(recommendProduct);
        }
        return recommendSubjectList.size();
    }

    @Override
    public int updateSort(Long id, Integer sort) {
        HomeRecommendSubject recommendProduct = new HomeRecommendSubject();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        return recommendProductMapper.updateById(recommendProduct);
    }

    @Override
    public int delete(List<Long> ids) {
        return recommendProductMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<HomeRecommendSubject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(HomeRecommendSubject::getId,ids);
        updateWrapper.set(HomeRecommendSubject::getRecommendStatus,recommendStatus);
        HomeRecommendSubject record = new HomeRecommendSubject();
        record.setRecommendStatus(recommendStatus);
        return recommendProductMapper.update(record,updateWrapper);
    }

    @Override
    public Page<HomeRecommendSubject> list(String subjectName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<HomeRecommendSubject> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        //满足条件再拼接
        queryWrapper.like(!StringUtils.isEmpty(subjectName),HomeRecommendSubject::getSubjectName,subjectName);
        queryWrapper.eq(recommendStatus!=null,HomeRecommendSubject::getRecommendStatus,recommendStatus);
        //按照Sort值排序
        queryWrapper.orderByDesc(HomeRecommendSubject::getSort);
        return recommendProductMapper.selectPage(page,queryWrapper);
    }
}
